//
//  FeedTableViewCell.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/3/14.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "FeedTableViewCell.h"
#define MARGIN 10

@implementation FeedTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    _avator = [[UIImageView alloc]initWithFrame:CGRectMake(MARGIN, MARGIN, 30, 30)];
//    _avator.image = [UIImage imageNamed:@"默认64.png"];
    
    _nameAndDevote = [[UILabel alloc]initWithFrame:CGRectMake(_avator.llz_right + MARGIN, MARGIN, 100, 30)];
    _nameAndDevote.font = FONT_PingFangSC_Thin_14;
//    _nameAndDevote.text = @"小铭(100)";
    
    _follow = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 60, MARGIN, 50, 30)];
    [_follow setTitle:@"关注" forState:UIControlStateNormal];
    [_follow setTitleColor:LLZRGBA(254, 205, 82, 1) forState:UIControlStateNormal];
    [_follow addTarget:self action:@selector(touchFollowBtn) forControlEvents:UIControlEventTouchUpInside];
    _follow.layer.borderWidth = 1;
    _follow.layer.borderColor = LLZRGBA(254, 205, 82, 1).CGColor;
    
    _titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30 + 2*MARGIN)];
    _titleView.backgroundColor = [UIColor whiteColor];
    [_titleView addSubview:_avator];
    [_titleView addSubview:_nameAndDevote];
    [_titleView addSubview:_follow];
    
//    while (1) {
//        if (_photos.count > 1) {
//            CGFloat itemSize = SCREEN_WIDTH/3 -20;
//            if (_photos.count < 3) {
//                _postView = [[UIView alloc]initWithFrame:CGRectMake(0, _titleView.llz_bottom, SCREEN_WIDTH, itemSize + 40)];
//            }else if (_photos.count < 6){
//                _postView = [[UIView alloc]initWithFrame:CGRectMake(0, _titleView.llz_bottom, SCREEN_WIDTH, itemSize * 2 + 5 + 40)];
//            }else{
//                _postView = [[UIView alloc]initWithFrame:CGRectMake(0, _titleView.llz_bottom, SCREEN_WIDTH, itemSize * 3 + 10 + 40)];
//            }
//
//            for (int i = 0; i < _photos.count; i++) {
//                if (i >= 3 && i <= 5) {//第二行图片
//                    _photos[i] = [[UIImageView alloc]initWithFrame:CGRectMake(5 +(i%3)*itemSize, itemSize + 5, itemSize, itemSize)];
//                }else if (i >= 6 && i <= 8){//第三行图片
//                    _photos[i] = [[UIImageView alloc]initWithFrame:CGRectMake(5 +(i%3)*itemSize, (itemSize + 5) * 2, itemSize, itemSize)];
//                }else{//第一行图片
//                    _photos[i] = [[UIImageView alloc]initWithFrame:CGRectMake(5 + (i%3)*itemSize, 0, itemSize, itemSize)];
//                }
//                [_postView addSubview:_photos[i]];
//            }
//            _introduce = [[UILabel alloc]initWithFrame:CGRectMake(5, _photos.lastObject.llz_bottom + 10, SCREEN_WIDTH, 30)];
//            _introduce.text = @"大家开始导航ID灰色空间打火机卡手段上课带回家阿克苏觉得好卡就好";
//            _introduce.numberOfLines = 0;
//
//            _postView.backgroundColor = [UIColor whiteColor];
//            [_postView addSubview:_introduce];
//            break;
//        }else{
//            _photos[0] = [[UIImageView alloc]init];
//            UIImage *image = [UIImage imageNamed:@"duck.jpg"];
//            _photos[0].image = image;
//            CGSize imageSize = [LLZHelper onePhotoRequestWithImage:image];
//            [_photos[0] setFrame:CGRectMake(5, 0, imageSize.width, imageSize.height)];
//
//            _introduce = [[UILabel alloc]initWithFrame:CGRectMake(5, _photos[0].llz_bottom + 10, SCREEN_WIDTH, 30)];
//            _introduce.text = @"大家开始导航ID灰色空间打火机卡手段上课带回家阿克苏觉得好卡就好";
//            _introduce.numberOfLines = 0;
//
//            _postView = [[UIView alloc]initWithFrame:CGRectMake(0, _titleView.llz_bottom, SCREEN_WIDTH, _photos[0].llz_height + 40)];
//            _postView.backgroundColor = [UIColor whiteColor];
//            [_postView addSubview:_photos[0]];
//            [_postView addSubview:_introduce];
//            break;
//        }
//    }

    _postView = [[UIView alloc]initWithFrame:CGRectMake(0, _titleView.llz_bottom, SCREEN_WIDTH, 100)];
    _postView.backgroundColor = [UIColor whiteColor];
    [_postView addSubview:_photos];
    [_postView addSubview:_introduce];
    
    UIImage *defaultImage = [UIImage imageNamed:@"duck.png"];
    _photos = [[UIImageView alloc]initWithImage:defaultImage];
    [_photos setFrame:CGRectMake(MARGIN, 0, 100, 100)];
    
    _introduce = [[UILabel alloc]initWithFrame:CGRectMake(MARGIN, _photos.llz_bottom + MARGIN, SCREEN_WIDTH, 30)];
//    _introduce.text = @"这是我今天拍的，大家觉得如何？";
    _introduce.numberOfLines = 0;
    
    _location = [[UILabel alloc]initWithFrame:CGRectMake(MARGIN, 0, 150, 30)];
    _location.font = FONT_PingFangSC_Thin_14;
    _location.text = _model.location;
    
    _time = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 150 - MARGIN, 0, 150, 30)];
    _time.font = FONT_PingFangSC_Thin_14;
    _time.textAlignment = NSTextAlignmentRight;
    _time.text = _model.time;
    
    _extraView = [[UIView alloc]initWithFrame:CGRectMake(0, _postView.llz_bottom, SCREEN_WIDTH, 30)];
    _extraView.backgroundColor = [UIColor whiteColor];
    [_extraView addSubview:_location];
    [_extraView addSubview:_time];
    
    _money = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/3, 30)];
    [_money setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_money addTarget:self action:@selector(touchMoneyBtn) forControlEvents:UIControlEventTouchUpInside];
    [_money setTitle:@"100" forState:UIControlStateNormal];
    _money.layer.borderColor = LLZRGBA(100, 100, 100, 1).CGColor;
    
    _thumb = [[UIButton alloc]initWithFrame:CGRectMake(_money.llz_right, 0, SCREEN_WIDTH/3, 30)];
    [_thumb setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_thumb addTarget:self action:@selector(touchThumbBtn) forControlEvents:UIControlEventTouchUpInside];
    [_thumb setTitle:@"👍102" forState:UIControlStateNormal];
    _thumb.layer.borderColor = LLZRGBA(100, 100, 100, 1).CGColor;
    
    _comment = [[UIButton alloc]initWithFrame:CGRectMake(_thumb.llz_right, 0, SCREEN_WIDTH/3, 30)];
    [_comment setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [_comment setBackgroundColor: [UIColor greenColor]];
    [_comment addTarget:self action:@selector(touchCommentBtn) forControlEvents:UIControlEventTouchUpInside];
    [_comment setTitle:@"✉️100" forState:UIControlStateNormal];
    _comment.layer.borderColor = LLZRGBA(100, 100, 100, 1).CGColor;
    
    _toolView = [[UIView alloc]initWithFrame:CGRectMake(0, _extraView.llz_bottom, SCREEN_WIDTH, 30)];
    _toolView.backgroundColor = [UIColor whiteColor];
    [_toolView addSubview:_money];
    [_toolView addSubview:_thumb];
    [_toolView addSubview:_comment];
    
    [self.contentView addSubview:_titleView];
    [self.contentView addSubview:_postView];
    [self.contentView addSubview:_extraView];
    [self.contentView addSubview:_toolView];
    
    self.backgroundColor = [UIColor whiteColor];
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(FeedModel *)model{
    _model = model;
    _nameAndDevote.text = [NSString stringWithFormat:@"%@(%@)",model.name,model.devote];
    _location.text = model.location;
    _time.text = model.time;
    _introduce.text = model.postContent;
    _model.topImageHeight = model.topImageHeight;
    _model.topImageWidth = model.topImageWidth;
    _avator.image = [UIImage imageNamed:model.imageName];
    _photos.image = [UIImage imageNamed:model.photos.firstObject];
}

- (CGSize)sizeThatFits:(CGSize)size {
    CGFloat totalHeight = 0;
    totalHeight += [_titleView sizeThatFits:size].height;
    totalHeight += [_postView sizeThatFits:size].height;
    totalHeight += [_extraView sizeThatFits:size].height;
    totalHeight += [_toolView sizeThatFits:size].height;
    return CGSizeMake(size.width, totalHeight);
}
///关注按钮被点击
- (void)touchFollowBtn{
    if ([_delegate respondsToSelector:@selector(followBtnClicked:)]) {
        [_delegate followBtnClicked:self];
    }
}

///价值按钮被点击
- (void)touchMoneyBtn{
    if ([_delegate respondsToSelector:@selector(moneyBtnClicked:)]) {
        [_delegate moneyBtnClicked:self];
    }
}

///点赞按钮被点击
- (void)touchThumbBtn{
    if ([_delegate respondsToSelector:@selector(thumbBtnClicked:)]) {
        [_delegate thumbBtnClicked:self];
    }
}

///评论按钮被点击
- (void)touchCommentBtn{
    if ([_delegate respondsToSelector:@selector(commentBtnClicked:)]) {
        [_delegate commentBtnClicked:self];
    }
}
@end
